<?php

header('Content-type: text/html; charset=utf-8');
error_reporting(0);
include_once 'DatabaseHelper.php';
require_once 'template.php';

//Start session
session_start();
if (!isset($_SESSION['SESS_MEMBER_ID']) || (trim($_SESSION['SESS_MEMBER_ID']) == '')) {

    die("Vui lòng đăng nhập để thực hiện chức năng này!! <br/>
        <a href='index.php'> Trở về trang chủ </a>");
}

if (isset($_POST['add_category'])) {
    DatabaseHelper::getInstance()->connect();

    $new_category = htmlspecialchars(strip_tags($_POST['new_category']));
    $insertQuery = "INSERT INTO category (category_id, category_name) 
                    VALUES (NULL,'$new_category')";
    $result = DatabaseHelper::getInstance()->query($insertQuery);
    header("location: category.php");
} elseif (isset($_POST['edit_category'])) {

    DatabaseHelper::getInstance()->connect();
    $query = "SELECT * FROM category";
    $result = DatabaseHelper::getInstance()->query($query);

    $row_cate_html = "";

    while ($row = mysql_fetch_array($result)) {

        $id = $row['category_id'];
        $cateName = $row['category_name'];

        $num_post_query = "SELECT count(id)as num_post FROM post where category = $id";
        $num_post_result = DatabaseHelper::getInstance()->query($num_post_query);

        $num_post = 0;
        while ($num_post_row = mysql_fetch_array($num_post_result)) {
            $num_post = $num_post_row['num_post'];
        }

        if (isset($_POST["remove-$id"]) &&
                $_POST["remove-$id"] == '1' &&
                $num_post == 0) {

            $delete_query = "DELETE FROM category where category_id = $id";
            DatabaseHelper::getInstance()->query($delete_query);
        } else {

            if (isset($_POST["rename-$id"])) {
                $rename_cate = htmlspecialchars(strip_tags($_POST["rename-$id"]));

                if ($rename_cate != "") {
                    $update_cateName_query = "UPDATE category SET category_name = '$rename_cate' where category_id = $id";
                    echo $update_cateName_query;
                    DatabaseHelper::getInstance()->query($update_cateName_query);
                }
            }
        }
    }
    
    DatabaseHelper::getInstance()->close();
    
    
    header("location: category.php");
} else {

    DatabaseHelper::getInstance()->connect();
    $query = "SELECT * FROM category";
    $result = DatabaseHelper::getInstance()->query($query);

    $row_cate_html = "";

    while ($row = mysql_fetch_array($result)) {

        $id = $row['category_id'];
        $cateName = $row['category_name'];

        $num_post_query = "SELECT count(id)as num_post FROM post where category = $id";
        $num_post_result = DatabaseHelper::getInstance()->query($num_post_query);

        $num_post = 0;
        while ($num_post_row = mysql_fetch_array($num_post_result)) {
            $num_post = $num_post_row['num_post'];
        }
        $disable_checkbox = "";

        if ($num_post > 0)
            $disable_checkbox = "disabled='disabled'";

        $row_cate_html .= "
<tr>
    <td class='ptitle'><a href='index.php?c=$id'> $cateName </a></td>
    <td><input type='text' name='rename-$id' value='' class='cat_rename' /></td>
    <td>$num_post</td>
    <td class='last'> <span " . (($num_post > 0) ? "class='dis'" : "") . ">
            <input type='checkbox' name='" . (($num_post == 0) ? "remove-$id" : "") . "' class='cat_remove' value='" . (($num_post == 0) ? "1" : "") . "'" . $disable_checkbox . " /> Xóa</span></td>
</tr>";
    }

    DatabaseHelper::getInstance()->close();

    $header = new Page('template/header-tpl.php');
    $footer = new Page('template/footer-tpl.php');

    $table_cate = new Page('template/category-table-tpl.php');
    $table_cate->replace_tags(array(
        "category_rows" => $row_cate_html
    ));
    $mainpage = new Page('template/category-tpl.php');

    $mainpage->replace_tags(array(
        "header" => $header->page,
        "footer" => $footer->page,
        "category_table" => $table_cate->page
    ));

    $mainpage->output();
}
?>
